package online.yangcloud.web.service.meta.impl;

import com.mybatisflex.core.query.QueryWrapper;
import online.yangcloud.common.annotation.TimeConsuming;
import online.yangcloud.common.enumration.YesOrNoEnum;
import online.yangcloud.common.tools.ExceptionTools;
import online.yangcloud.core.mapper.GlobalMapper;
import online.yangcloud.core.model.entity.DocumentEntity;
import online.yangcloud.web.service.meta.DocumentMetadataService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhuboyang
 * @since 2023年10月20 11:15:07
 */
@Service
@Transactional(rollbackFor = Exception.class)
@TimeConsuming
public class DocumentMetadataServiceImpl implements DocumentMetadataService {

    @Resource
    private GlobalMapper globalMapper;

    @Override
    public void addDocumentRecord(DocumentEntity metadata) {
        ExceptionTools.updateError(globalMapper.acquireDocument().insertWithPk(metadata));
    }

    @Override
    public DocumentEntity queryDocumentByFileId(String id) {
        return globalMapper.acquireDocument().selectOneByQuery(QueryWrapper.create()
                .where(DocumentEntity::getFileId).eq(id)
                .and(DocumentEntity::getIsDelete).eq(YesOrNoEnum.NO.code()));
    }

    @Override
    public List<DocumentEntity> queryDocumentsByFileIds(List<String> fileIds) {
        return globalMapper.acquireDocument().selectListByQuery(QueryWrapper.create()
                .where(DocumentEntity::getFileId).in(fileIds)
                .and(DocumentEntity::getIsDelete).eq(YesOrNoEnum.NO.code()));
    }

}
